package common.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Administrator on 2016/7/30.
 */
public class Interceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 是否跳转到访问的请求
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @return
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
   /*     String pageNum = httpServletRequest.getParameter("pageNum").toString();
        //mabatis分页
        PageHelper.offsetPage(1, 4);
        //hibernate 分页
        Page page = PageThreadLocal.getPage();
        page.setPageNum(2);
        PageThreadLocal.setPage(page);*/

        //  String body = IOUtils.toString(httpServletRequest.getInputStream());
        HandlerMethod handlerMethod = (HandlerMethod) o;
        for (MethodParameter methodParameter : handlerMethod.getMethodParameters()) {
            System.out.println(methodParameter.getParameterName());

        }
     /*   if (redisTemplate.opsForSet().isMember("sessionIds", httpServletRequest.getSession().getId())) {
            return true;
        } else
            httpServletResponse.sendRedirect("/sign-in.jsp");*/
        return true;
    }

    /**
     * 返回修改
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param modelAndView
     * @throws Exception
     */
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    /**
     * 请求结束调用该方法(资源回收等)
     *
     * @param httpServletRequest
     * @param httpServletResponse
     * @param o
     * @param e
     * @throws Exception
     */
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
